package com.kitty.game.confirm.service.handler;

import com.kitty.common.utils.SpringUtils;
import com.kitty.game.bag.message.RespIconCartoon;
import com.kitty.game.config.Fasion;
import com.kitty.game.confirm.model.BuyFashionConfirm;
import com.kitty.game.equip.message.RespNotifyMiscEx;
import com.kitty.game.equip.model.RoleEquip;
import com.kitty.game.equip.service.EquipService;
import com.kitty.game.function.service.FasionService;
import com.kitty.game.role.model.Role;
import com.kitty.game.role.service.RoleService;
import com.kitty.game.team.message.ReqConfirmResult;
import com.kitty.game.waiguan.RespFasionCustomBegin;
import com.kitty.game.waiguan.RespFasionCustomEnd;
import com.kitty.common.model.Reason;
import com.kitty.mina.message.MessagePusher;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 处理购买时装
 */
@Component
public class BuyFashionConfirmHandler extends ConfirmHandler {
    @Autowired
    RoleService roleService;
    @Autowired
    EquipService equipService;
    @Autowired
    FasionService fasionService;

    @Override
    public void handleConfirmResult(Role role, ReqConfirmResult reqConfirmResult) {
        if ("1".equals(reqConfirmResult.getSelect())) {
            buyFasion(role);
        }
    }

    /**
     * 购买自定义时装
     */
    private void buyFasion(Role role) {
        BuyFashionConfirm confirm = (BuyFashionConfirm) role.getConfirm();
        Fasion fasion = confirm.getFasion();
        int needGold = fasion.getPrice();
        if (role.getGold() < needGold || needGold < 0) {
            return;
        }

        RespFasionCustomBegin respFasionCustomBegin = new RespFasionCustomBegin();
        respFasionCustomBegin.setLabel("buy_fasion");
        MessagePusher.pushMessage(role, respFasionCustomBegin);
        SpringUtils.getRoleService().subtractGold(role, fasion.getPrice(), Reason.BUY_EFFECT);
        roleService.updateRoleGoldAndSiver(role);

        short pos = fasionService.getFasionPos(role, "fasion_store");
        RoleEquip roleEquip = fasionService.getFasion(role, fasion.getName(), 1, true);
        roleEquip.setPosition(pos);
        equipService.add(role, roleEquip);

        MessagePusher.pushMessage(role, new RespIconCartoon(fasion.getName()));
        roleService.updateRoleGoldAndSiver(role);
        fasionService.sendFasionStore(roleEquip.getPosition(), role, roleEquip, "fasion_store");

        MessagePusher.pushMessage(role, new RespNotifyMiscEx("你花费#R" + fasion.getPrice() + "#n个金元宝购买了#R" + fasion.getName() + "#n。"));

        roleService.updateRoleGoldAndSiver(role);
        RespFasionCustomEnd respFasionCustomEnd = new RespFasionCustomEnd();
        respFasionCustomEnd.setLabel("buy_fasion");
        MessagePusher.pushMessage(role, respFasionCustomEnd);
    }

    /**
     * 背包使用时装  //任务奖励时装
     */
    public void rewardFasionWanDao(Role role ) {
        //获取当前人证道等级
        int zhengdaoLevel = role.getExtendBox().getZhengdaoLevel();
        String fasionName = getNameByIndex(zhengdaoLevel, role.getGender());
        //购买开始
        RespFasionCustomBegin respFasionCustomBegin = new RespFasionCustomBegin();
        respFasionCustomBegin.setLabel("buy_fasion");
        MessagePusher.pushMessage(role, respFasionCustomBegin);


        short pos = fasionService.getFasionPos(role, "fasion_store");
        RoleEquip roleEquip = fasionService.getFasion(role, fasionName, 1, true);
        roleEquip.setPosition(pos);
        equipService.add(role, roleEquip);
        MessagePusher.pushMessage(role, new RespIconCartoon(fasionName));

        //购买结束
        RespFasionCustomEnd respFasionCustomEnd = new RespFasionCustomEnd();
        respFasionCustomEnd.setLabel("buy_fasion");
        MessagePusher.pushMessage(role, respFasionCustomEnd);
    }

    //根据证道等级获取时装名称
    public static String getNameByIndex(int index, int sex) {
        String name = "";
        switch (index) {
            case 1:
                name = "万道袍·一元初始";
                break;
            case 3:
                name = "万道袍·两仪阴阳";
                break;
            case 5:
                name = "万道袍·三才合一";
                break;
            case 7:
                name = "万道袍·四象轮回";
                break;
            case 9:
                name = "万道袍·五行聚源";
                break;
            case 11:
                name = "万道袍·六合天更";
                break;
            case 13:
                name = "万道袍·七星渡世";
                break;
        }
        if (sex == 1) {
            return name;
        } else {
            name = name.replaceAll("袍","衣");
            return name;
        }
    }

    public static void main(String[] args) {

        System.out.println( getNameByIndex(2,1));
    }
}
